package xyz.kongbai121.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.kongbai121.demo.domain.*;
import xyz.kongbai121.demo.mapper.*;
import xyz.kongbai121.demo.service.IUserService;
import xyz.kongbai121.demo.util.UserPowerException;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private TreeMapper treeMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private OpinionMapper opinionMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public User info(String token) {
        String openid = redisTemplate.opsForValue().get(token);
        if (openid == null || openid.isEmpty())  throw new UserPowerException("请先登录");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public List<Article> articlesListByUser(String token) {
        String openid = redisTemplate.opsForValue().get(token);
        if (openid == null || openid.isEmpty()) throw new UserPowerException("请先登录");

        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        wrapper.orderByDesc("release_time");
        List<Article> list = articleMapper.selectList(wrapper);
        for (Article article : list) {
            String goodNum = redisTemplate.opsForValue().get("articleGoodNum"+article.getId());
            String comment = redisTemplate.opsForValue().get("articleCommentNum"+article.getId());
            if (goodNum == null||goodNum.equals("null")) goodNum = "0";
            if (comment == null||comment.equals("null")) comment = "0";
            article.setGoodNum(Integer.valueOf(goodNum));
            article.setCommentNum(Integer.valueOf(comment));
        }
        return list;
    }

    @Override
    public List<Tree> treeListByUser(String token) {
        String openid = redisTemplate.opsForValue().get(token);
        if (openid == null || openid.isEmpty()) throw new UserPowerException("请先登录");
        QueryWrapper<Tree> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        wrapper.orderByDesc("release_time");
        List<Tree> list = treeMapper.selectList(wrapper);
        for (Tree tree : list) {
            String goodNum = redisTemplate.opsForValue().get("treeGoodNum"+tree.getId());
            String comment = redisTemplate.opsForValue().get("treeOpinionNum"+tree.getId());
            if (goodNum == null||goodNum.equals("null")) goodNum = "0";
            if (comment == null||comment.equals("null")) comment = "0";
            tree.setGoodNum(Integer.valueOf(goodNum));
            tree.setOpinionNum(Integer.valueOf(comment));
        }
        return list;
    }

    @Override
    public Integer getArticleNum(String token) {
        String openid = redisTemplate.opsForValue().get(token);
        if (openid == null || openid.isEmpty()) throw new UserPowerException("请先登录");
        return userMapper.getArticleNum(openid);
    }

    @Override
    public Integer getTreeHoleNum(String token) {
        String openid = redisTemplate.opsForValue().get(token);
        if (openid == null || openid.isEmpty()) throw new UserPowerException("请先登录");
        return userMapper.getTreeHoleNum(openid);
    }

    @Override
    public Integer getNewCommentNum(String token) {
        String openid = redisTemplate.opsForValue().get(token);
        if (openid == null || openid.isEmpty()) throw new UserPowerException("请先登录");
        return userMapper.getUnreadComment(openid);
    }

    @Override
    public List<Comment> getCommentList(String token) {
        String openid = redisTemplate.opsForValue().get(token);
        if (openid == null || openid.isEmpty()) throw new UserPowerException("请先登录");
        userMapper.emptyUnreadComment(openid);
        commentMapper.hasRead(openid);
        return commentMapper.commentListByUser(openid);
    }

    @Override
    public Integer getNewOpinionNum(String token) {
        String openid = redisTemplate.opsForValue().get(token);
        if (openid == null || openid.isEmpty()) throw new UserPowerException("请先登录");
        return userMapper.getUnreadOpinion(openid);
    }

    @Override
    public List<Opinion> getOpinionList(String token) {
        String openid = redisTemplate.opsForValue().get(token);
        if (openid == null || openid.isEmpty()) throw new UserPowerException("请先登录");
        userMapper.emptyUnreadOpinion(openid);
        opinionMapper.hasRead(openid);
        return opinionMapper.opinionListByUser(openid);
    }

    @Override
    public Long getUserId(String token) {
        String openid = redisTemplate.opsForValue().get(token);
        if (openid == null || openid.isEmpty()) throw new UserPowerException("请先登录");
        return userMapper.getUserId(openid);
    }
}
